package com.dykj.fireFighting.web.base.dev.controller;

import java.util.*;

import com.dykj.fireFighting.api.base.dev.entity.DevHisSs;
import com.dykj.fireFighting.api.base.dev.entity.dto.DevHisSsDto;
import com.dykj.fireFighting.api.base.dev.service.IDevHisSsService;
import com.dykj.fireFighting.api.elasticsearch.devHisSs.entity.EsDevHisSs;
import com.dykj.fireFighting.api.elasticsearch.devHisSs.service.EsDevHisSsService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.dto.CommonDto;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;



import javax.validation.Valid;



/**
 * 盛善运行记录接口
 *
 * @Author JacksonTu
 * @Date  2021-12-02 10:35:51
 */
@Slf4j
@Api(value = "盛善运行记录接口", tags = {"盛善运行记录接口"})
@RestController
@RequestMapping("devHisSs")
public class DevHisSsController {

    @DubboReference
    private IDevHisSsService devHisSsService;
    @DubboReference
    private EsDevHisSsService esDevHisSsService;


    /**
     * 盛善运行记录列表
     *
     * @return
     */
    @AutoLog(value = "盛善运行记录-分页列表查询")
    @ApiOperation(value = "盛善运行记录分页列表", notes = "盛善运行记录分页列表")
    @GetMapping("/list")
   /* @RequiresPermissions("devHisSs/list")*/
    public CommonResult<PageVo<EsDevHisSs>> treeGrid(DevHisSsDto devHisSsDto,@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                   @RequestParam(required = false, defaultValue = "10") Integer pageSize) {


        LoginUserVo user = ShiroUtil.getUser();
        /**
         * 单位类型(社会单位:0，维保单位:1，监控中心:2)
         */
        Integer enterpriseType = user.getEnterpriseType();
        if(0 == enterpriseType){
            devHisSsDto.setUnitId(user.getEnterpriseId().toString());
        }
        if(2 == enterpriseType){
            devHisSsDto.setCenterUnitId(user.getEnterpriseId().toString());
        }
        PageVo<EsDevHisSs> page = esDevHisSsService.queryList(devHisSsDto.getSj(), devHisSsDto.getStatus(), devHisSsDto.getUnitId(),devHisSsDto.getCenterUnitId(), pageNum, pageSize, devHisSsDto.getDevName(), devHisSsDto.getDevType());
        return CommonResult.success(page);
    }

    /**
     * 获取盛善运行记录信息
     */
    @AutoLog(value = "盛善运行记录-获取盛善运行记录信息")
    @ApiOperation(value = "获取盛善运行记录信息", notes = "获取盛善运行记录信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    @RequiresPermissions("sms/devHisSs/info")
    public CommonResult<DevHisSs> info(@PathVariable("id") String id){
        DevHisSs devHisSs = devHisSsService.getById(id);
        return CommonResult.success(devHisSs);
    }

    /**
     * 保存盛善运行记录信息
     */
    @AutoLog(value = "盛善运行记录-保存盛善运行记录信息")
    @ApiOperation(value = "保存盛善运行记录信息", notes = "保存盛善运行记录信息")
    @PostMapping("/save")
    @RequiresPermissions("sms/devHisSs/save")
    public CommonResult save(@Valid @RequestBody DevHisSs devHisSs) {
        try {
           /* devHisSs.setCreateTime(new Date());
            devHisSs.setUpdateTime(new Date());*/
                devHisSsService.save(devHisSs);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改盛善运行记录信息
     */
    @AutoLog(value = "盛善运行记录-修改盛善运行记录信息")
    @ApiOperation(value = "修改盛善运行记录信息", notes = "修改盛善运行记录信息")
    @PostMapping("/update")
    @RequiresPermissions("sms/devHisSs/update")
    public CommonResult update(@Valid @RequestBody DevHisSs devHisSs) {
        try {
//            devHisSs.setUpdateTime(new Date());
                devHisSsService.updateById(devHisSs);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除盛善运行记录信息
     */
    @AutoLog(value = "盛善运行记录-删除盛善运行记录信息")
    @ApiOperation(value = "删除盛善运行记录信息", notes = "删除盛善运行记录信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    @RequiresPermissions("sms/devHisSs/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
                devHisSsService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }
}
